let timer;
let timeNum = 0;
const app = getApp();
import { goLogin } from '../../utils/util.js'
Page({
  data: {
    deviceid: null,
    curModule: null,
    canSendMsg: true,
    showDialog: '',
    tokenError: false, 
    showMask: true,
    deviceList: '', // 设备对象 最后一节点
    showPicker: false
  },
  onLoad: function (options) {
    let moduleList = wx.getStorageSync('moduleList');
    this.setData({
      curModule: moduleList[0],
      deviceid: options.deviceid,
      deviceList: app.globalData.deviceList
    })
    app.watch('showPicker', (value) => {
      this.setData({
        showPicker: value
      });
    })
  },
  onShow(){
    timeNum = 0;
    let curModule = this.data.curModule;
    if (curModule.moduleID !== "health_tobaccoxiamen" && curModule.moduleID !== 'oee_widia' && curModule.moduleID !== 'health' && curModule.moduleID !== 'health_comac' && curModule.moduleID !== 'health_motor') return
   
    if(!app.globalData.isOpen){
      app.initSocket();
    }
    this.sendSocket()
    wx.setStorageSync('canRequest', false)
  },
  // 页面卸载，页面回退的时候
  onUnload(){
    // let cmdArr = ['sts', 'daily', 'runtime', 'history', 'status'];
    let cmdArr;
    cmdArr = ['assessment', 'assessmenta', 'healthcurve', 'ahealthcurve', 'trendcurve','atrendcurve', 'runtime', 'daystate', 'weekstate']
    cmdArr.forEach(item => {
      app.openSendMsg({
        cmd: item,
        start: false
      })
    })
    app.onHide();
  },
  // 发送数据给后台
  sendSocket() {
    clearTimeout(timer)
    if (!app.globalData.isOpen) {
      timer = setTimeout(() => {
        this.sendSocket()

        timeNum++;
        

        if (timeNum > 5) {
          clearTimeout(timer)

          wx.showToast({
            title: '登录超时，请重新登录！',
            icon: 'none',
            duration: 800
          })

          wx.clearStorageSync('token');
          wx.clearStorageSync('functionModule');
          wx.clearStorageSync('radioChange')

          setTimeout(() => {
            app.onShow()
            wx.reLaunch({
              url: '/pages/login/login'
            })
          }, 1000)

        }

      
      }, 1000)
    } else {
      this.getSocketMsg()
    }
  },

  getSocketMsg(){
    let cmdArr;
    cmdArr = ['assessment', 'assessmenta', 'healthcurve', 'ahealthcurve', 'trendcurve', 'atrendcurve', 'runtime', 'daystate', 'weekstate']
    let data = {
        token: wx.getStorageSync('token'),
        userName: wx.getStorageSync('username'),
        start: true,
        deviceID: this.data.deviceid
    };
    let that = this;
    cmdArr.forEach(item => {
      // 清除websocket
      app.openSendMsg({
        cmd: item,
        start: false
      })
      data.cmd = item;
      app.openSendMsg(data)
      app.globalData.callback = function (res) {
        let data = {};
        try {
          data=JSON.parse(res.data);
        } catch (error) {
          console.log(res.data)
        }
        if (data.status === '0' && !that.data.tokenError){
          that.setData({
            tokenError: true
          })
          goLogin();
          return
        }
        if (data.resCode !== "SUCCESS") return
        let child = that.selectComponent(".child")
        child && child.setEcharts(data)
      }
    })
  },

  // nav 调用隐藏canvas
  changeShow(e){
    this.setData({
      showDialog: e.detail.showDialog
    })
  },

  // bar切换
  changeBar( e ){
    this.setData({
      curModule: e.detail.curModule
    })
    if (this.data.curModule.moduleID !== "health_tobaccoxiamen" && this.data.curModule.moduleID !== 'oee_widia' && this.data.curModule.moduleID !== 'health' && this.data.curModule.moduleID !== 'health_comac' && this.data.curModule.moduleID !== 'health_motor') return
    this.sendSocket()
  },


  // 调用子组件
  sendSocketMsg(e){
    app.onHide();
    timeNum=0;
    if(!app.globalData.isOpen){
      app.initSocket();
    }
    this.setData({
      deviceid: e.detail.deviceID
    })
    this.selectComponent(".history") && this.selectComponent(".history").selectComponent('.comac_history') && this.selectComponent(".history").selectComponent('.comac_history').getListByNav(e.detail.deviceID);
    this.selectComponent(".event") && this.selectComponent(".event").getEventByPa();
    this.selectComponent(".history") && this.selectComponent(".history").dispatchHistory();   
    this.selectComponent(".child") && this.selectComponent(".child").selectComponent(".health-time") && this.selectComponent(".child").selectComponent(".health-time").getCateOption();  
    this.sendSocket()
  },
  onReachBottom () { 
    if (this.data.curModule.moduleID !== "anomalous_event") return;
    let eventDom = this.selectComponent('#anomalous_event');
    eventDom.reachBottom()
  }
 
})